package com.hazardSoft.eStore.bussines;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Offer;
import com.hazardSoft.eStore.utils.PathConstants;
import com.hazardSoft.eStore.utils.QueryConstants;

public class OfferBO {

	private static final String CLASS_NAME = PathConstants.BUSSINES_PATH+OfferBO.class.getSimpleName();

	public Offer insertOffer(Offer offer, EntityManager entityManager) throws SystemException{
		if (this.searchOfferByName(offer, entityManager) == null) {
			if (offer.getId() == null) {
				offer.setId(0);
			}
			entityManager.persist(offer);
			return this.searchOfferByName(offer, entityManager);
		}
		return null;
	}

	public Offer searchOfferByName(Offer offer, EntityManager entityManager) throws SystemException{
		if (offer == null) {
			throw new SystemException(new Exception("null offer"), CLASS_NAME, "searchOfferByName");
		}
		if (offer.getName() == null) {
			throw new SystemException(new Exception("null offer.name"), CLASS_NAME, "searchOfferByName");
		}
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_OFFER_BY_NAME);
		query.setParameter(1, offer.getName().toLowerCase());

		List<Offer> offerList = query.getResultList();

		if (offerList.size() > 0) {
			return offerList.get(0);
		}
		return null;
	}

	public boolean removeOffer( Offer offer, EntityManager entityManager ) throws SystemException{
		if (offer == null) {
			throw new SystemException(new Exception("null offer"), CLASS_NAME, "removeOffer");
		}
		if (!(offer.getName() == null)) {
			offer = this.searchOfferByName(offer, entityManager);
			entityManager.remove(offer);
			return true;
		} else {
			if (!(offer.getId() == null)) {
				offer = this.searchOfferById(offer, entityManager);
				entityManager.remove(offer);
				return true;
			} else {
				throw new SystemException(new Exception("null offer.id or offer.name"), CLASS_NAME, "removeOffer");
			}
		}
	}

	public Offer searchOfferById(Offer offer, EntityManager entityManager) throws SystemException{
		if (offer == null) {
			throw new SystemException(new Exception("null offer"), CLASS_NAME, "searchOfferById");
		}

		if (!(offer.getId() == null)) {
			return entityManager.find(Offer.class, offer.getId());
		} else {
			throw new SystemException(new Exception("null offer.id"), CLASS_NAME, "searchOfferById");
		}
	}

	public Offer updateOffer(Offer offer, EntityManager entityManager) throws SystemException{
		Offer tempOffer = this.searchOfferByName(offer, entityManager); 
		if (!(tempOffer == null)) {
			if (!(offer.getConditions() == null)) {
				tempOffer.setConditions(offer.getConditions());
			}
			if(!(offer.getDescription() == null)) {
				tempOffer.setDescription(offer.getDescription());
			}
			if (!(offer.getName() == null)) {
				tempOffer.setName(offer.getName());
			}
			if(!(offer.getOffertype() == null)) {
				tempOffer.setOffertype(offer.getOffertype());
			}
			if (!(offer.getPorcentage() == null)) {
				tempOffer.setPorcentage(offer.getPorcentage());
			}

			if(offer.getQuantity() == null){
				if(!(offer.getEnddate() == null)) {
					tempOffer.setEnddate(offer.getEnddate());
				}
				if (!(offer.getStartdate() == null)) {
					tempOffer.setStartdate(offer.getStartdate());
				}
				tempOffer.setQuantity(null);//-----
			}
			else{
				tempOffer.setQuantity(offer.getQuantity());

				tempOffer.setEnddate(null);//----
				tempOffer.setStartdate(null);//----

			}

			return tempOffer;
		}
		return null;
	}

	public List<Offer> listOffers(EntityManager entityManager) throws SystemException {

		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_OFFERS);

		return query.getResultList();
	}
}
